import { Component, OnInit } from '@angular/core';
import { DatePipe } from '@angular/common';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router, ActivatedRoute, NavigationExtras} from '@angular/router';
import { SelladPositionService } from './sell-ad-position.service'
import {PermissionService} from '../../../service/permission.service';

@Component({
  selector: 'app-sell-ad-position',
  templateUrl: './sell-ad-position.component.html',
  styleUrls: ['./sell-ad-position.component.scss'],
  providers: [DatePipe]
})
export class SellAdPositionComponent implements OnInit {

  // seachForm
  seachForms: any = {
    name: '', // 广告位
    advertId: '', // 广告id
    state: '' // 状态 0：正常，1：已删除
  }
  // table
  tableForms: any = {
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0
  };
  // 表格data
  tableData: any = [];

  // 渠道list
  channelLists: any = [];
  // 业务list
  businessLists: any = [];

  constructor(
    private datePipe: DatePipe,
    private message: NzMessageService,
    private router: Router,
    private service: SelladPositionService,
    public permission: PermissionService,
  ) { }

  ngOnInit() {
    this.queryChannelData()
    this.queryBusinessData()

    this.loadTable()
  }

  // loaddata
  loadTable() {
    let params = {
      ...this.seachForms,
      page: this.tableForms.page,
      pageSize: this.tableForms.pageSize
    }

    this.tableForms.tableLoading = true;
    this.service.queryTableData(params).subscribe((res: any) => {
      this.tableForms.tableLoading = false;

      const { list, count } = res.data
      this.tableData = list || [];
      this.tableForms.total = count || 0;
    }, (error: any) => {
      this.tableForms.tableLoading = false;
      this.tableData = [];
      this.tableForms.total = 0;
    })
  }

  // 搜索
  searchData() {
    this.tableForms.page = 1;
    this.loadTable()
  }
  // 重置table
  resetData() {
    this.seachForms.name = ''; // 广告位
    this.seachForms.advertId = ''; // 广告id
    this.seachForms.state = ''; // 状态 0：启用，1：禁用

    this.tableForms.page = 1;
    this.loadTable()
  }

  /**
   * 页面跳转
   * @param num 0：新增,1：编辑,2：查看,3：复制
   * @param item 单项data
   */
  jumpPage(num: number, item?: any) {
    this.router.navigate(['/sell/adPositionSave'], {
      queryParams: {
        id: num == 0 ? '' : item.id,
        mode: num
      }
    })
  }

  // 确认删除
  deleteConfirm(id: any) {
    let params = {
      id: id,
      state: 1
    }

    this.service.modifyData(params).subscribe((res: any) => {
      if (res.code == 0) {
        this.message.success('删除成功');
      } else {
        this.message.success(res.message || '删除失败');
      }
      this.loadTable();
    }, (error: any) => {
      // console.log(error);
      this.message.success(error);
    })
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableForms.page = index;
    this.loadTable()
  }
  // 页数改变时回调
  onPageSizeChange(sizeNum: number) {
    this.tableForms.page = 1;
    this.tableForms.pageSize = sizeNum;

    this.loadTable()
  }

  // 查询渠道
  async queryChannelData() {
    let data: any = await this.queryDictionariesData('channel');
    this.channelLists = data || [];
  }

  // 查询业务
  async queryBusinessData() {
    let data: any = await this.queryDictionariesData('business');
    this.businessLists = data || []
  }

  /**
   * 字典查询
   * @param parentKey 父类字典编码
   */
  async queryDictionariesData(parentKey: any) {
    const res: any = await this.service.dictListData(parentKey);
    return res.data.list
  }
}
